package net.vanchu.mortgagecalculator.common.commonView.component;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dtspread.apps.houseloan.R;

import net.vanchu.mortgagecalculator.common.commonView.ViewLogger;

/**
 * 带有输入选项的组合 layout
 * <p>
 * Created by Granton on 2016/12/16.
 */
public class WritingOptionLayout extends RelativeLayout {

    private static final String TAG = WritingOptionLayout.class.getSimpleName();

    private TextView leftTextView;

    public WritingOptionLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public WritingOptionLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        View parentView = LayoutInflater.from(context).inflate(R.layout.layout_option_to_write, this, true);
        leftTextView = (TextView) parentView.findViewById(R.id.left_text);
        TextView rightTextView = (TextView) parentView.findViewById(R.id.right_text);
        EditText inputArea = (EditText) parentView.findViewById(R.id.input_area);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.OptionsLayout, 0, defStyleAttr);
        leftTextView.setText(a.getString(R.styleable.OptionsLayout_left_text));
        rightTextView.setText(a.getString(R.styleable.OptionsLayout_right_text));
        a.recycle();

        inputArea.setFilters(new InputFilter[]{
                new InputFilter() {
                    @Override
                    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                        Log.i(TAG, "InputFilter work");
                        final StringBuilder stringBuilder = new StringBuilder();
                        stringBuilder.append(dest, 0, dstart);
                        stringBuilder.append(source, start, end);
                        stringBuilder.append(dest, dend, dest.length());
                        if (!inputValidator.valid(stringBuilder.toString())) {
                            CharSequence result = dest.subSequence(dstart, dend);
                            ViewLogger.d(TAG, "invalid " + result.toString());
                            notifyInputStateListener(inputValidator.getErrorMsgContainer()[0]);
                            return result;
                        }
                        return null;
                    }
                }
        });
        inputArea.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                Log.i(TAG, "TextWatcher work");
                if (inputStateListener == null) {
                    return;
                }
                String input = s.toString();
                inputStateListener.onInputValid(WritingOptionLayout.this.getId(), input);
            }
        });
    }

    private void notifyInputStateListener(String errorMsg) {
        if (inputStateListener == null) {
            return;
        }
        inputStateListener.onInputInvalid(errorMsg);
    }

    public void setLeftTextViewText(int resId) {
        setLeftTextViewText(getContext().getString(resId));
    }

    public void setLeftTextViewText(String text) {
        this.leftTextView.setText(text);
    }

    private InputValidator inputValidator;

    private InputStateListener inputStateListener;

    public void setInputValidator(InputValidator inputValidator) {
        this.inputValidator = inputValidator;
    }

    public void setInputStateListener(InputStateListener inputStateListener) {
        this.inputStateListener = inputStateListener;
    }

    public interface InputValidator {
        boolean valid(String input);

        String[] getErrorMsgContainer();
    }

    public interface InputStateListener {
        void onInputValid(int id, String validText);

        void onInputInvalid(String errorMsg);
    }

}